package com.manner.groupon.service.store.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.exception.CustomException;
import com.manner.groupon.mapper.StoreAccountFlowMapper;
import com.manner.groupon.mapper.StoreLinkManMapper;
import com.manner.groupon.model.entity.GrouponOrder;
import com.manner.groupon.model.entity.Store;
import com.manner.groupon.model.entity.StoreAccountFlow;
import com.manner.groupon.model.entity.StoreLinkMan;
import com.manner.groupon.model.vo.StaffQueryStoreAccountFlowVO;
import com.manner.groupon.service.groupon.GrouponOrderService;
import com.manner.groupon.service.store.StoreAccountFlowService;
import com.manner.groupon.service.store.StoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author manner
 * @description 针对表[tb_store_account_flow(商家账面流水表)]的数据库操作Service实现
 * @createDate 2024-10-13 14:26:26
 */
@Service
@RequiredArgsConstructor
public class StoreAccountFlowServiceImpl extends ServiceImpl<StoreAccountFlowMapper, StoreAccountFlow> implements StoreAccountFlowService {

    private final StoreService storeService;
    private final GrouponOrderService grouponOrderService;
    private final StoreLinkManMapper storeLinkManMapper;
    private final StoreAccountFlowMapper storeAccountFlowMapper;


    /**
     * @param staffPhone 登录后台的员工手机号
     * @return StaffQueryStoreAccountFlowVO 过滤非必要的数据VO
     * @description: 查询该员工的所有流水记录
     * @author Manner
     * @date 2024/11/11 10:48
     */
    @Override
    public List<StaffQueryStoreAccountFlowVO> getAllFlow(String staffPhone) {
        //查询员工个人信息
        StoreLinkMan staffInfo = storeLinkManMapper.selectOne(new LambdaQueryWrapper<StoreLinkMan>()
                .eq(StoreLinkMan::getPhone, staffPhone));
        if (BeanUtil.isEmpty(staffInfo)) {
            throw new CustomException("员工不存在!");
        }
        List<StoreAccountFlow> storeAccountFlows = storeAccountFlowMapper.selectList(
                new LambdaQueryWrapper<StoreAccountFlow>()
                        .eq(StoreAccountFlow::getUserId, staffInfo.getId()));
        if (CollUtil.isEmpty(storeAccountFlows)) {
            throw new CustomException("流水信息为空!");
        }
        //准备过滤VO数据类型容器
        List<StaffQueryStoreAccountFlowVO> staffQueryStoreAccountFlowVOS = new ArrayList<>();
        //遍历新建VO拷贝
        storeAccountFlows.forEach(storeAccountFlow -> {
            staffQueryStoreAccountFlowVOS.add(BeanUtil.copyProperties(storeAccountFlow, StaffQueryStoreAccountFlowVO.class));
        });
        return staffQueryStoreAccountFlowVOS;
    }

    @Override
    public BaseResponse<List<StoreAccountFlow>> search(String phone) {
        //查询联系人数据
        StoreLinkMan linkMan = storeLinkManMapper.selectOne(new QueryWrapper<StoreLinkMan>().eq("phone", phone));
        //获取商家门店信息
        List<Store> list = storeService.query().eq("admin_id", linkMan.getId()).list();
        //转换为编号集合
        List<String> collect = list.stream().map(Store::getStoreNo).collect(Collectors.toList());
        //获取门店订单数据
        List<GrouponOrder> orders = grouponOrderService.query().in("store_no", collect).list();
        //转为订单编号
        List<String> ids = orders.stream().map(GrouponOrder::getGrouponOrderId).collect(Collectors.toList());
        //获取流水记录
        List<StoreAccountFlow> storeFlows = query().in("groupon_order_id", ids).orderByDesc("create_time").list();
        //返回数据
        return ResultUtils.success(storeFlows);
    }

}




